package com.wangku.springmvc.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.wangku.springmvc.entity.PageData;
import com.wangku.springmvc.entity.UserInfo;
import com.wangku.springmvc.service.WxTokenService;
import com.wangku.springmvc.utils.AppResponse;
import com.wangku.springmvc.utils.Const;
import com.wangku.springmvc.utils.redis.RedisPoolUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Map;

/**
 * @description: 微信小程序登录拦截器
 * @author: 李赛赛
 * @create: 2018-08-14 14:12
 **/
public class WeappletAuthorizationInterceptor implements HandlerInterceptor {
    //    端拦截路径
    public static final String INTERCEPTOR_PATH = ".*/((bank/*)(user/*)|(order/*)|(shop/product)|(shop/catagory)|(shop/company)|(notPass)).*";

    @Resource(name = "wxTokenService")
    private WxTokenService wxTokenService;
    Jedis jedis;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod method = (HandlerMethod) handler;
        String methodName = method.getBeanType().getName() + "." + method.getMethod().getName();

        // 登录，验证码放行 等方法放行
        if (methodName.equals("com.wangku.springmvc.controller.WxCheatLoginController.login")||
            methodName.equals("com.wangku.springmvc.controller.WxCheatLoginController.test")) {
            return true;
        }
//        String tokens = request.getParameter(Const.DTOKEN_NAME);
        String tokens = request.getHeader(Const.DTOKEN_NAME);
        Map<String, String> token = wxTokenService.getToken(tokens);
        if (!token.isEmpty()) {
            return true;
        }
        String errMsg;

        if (token == null||token.size()==0) {
            errMsg = "登录超时，请重新授权登录";
        } else {
            errMsg = "您的帐号尚未登录，请重新授权登录。";
        }
        responseOutWithJson(response, AppResponse.noPermission(errMsg));
        return false;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
    protected void responseOutWithJson(HttpServletResponse response,
                                       AppResponse appResponse) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(JSONObject.toJSONString(appResponse));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                //out.close();
            }
        }
    }
}
